﻿#


if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
	& 'c:\ProgramData\kasini3000\0k_source.ps1'
}

if ($IsLinux -eq $True)
{
	& '/etc/kasini3000/0k_source.ps1'
}

$private:temp999 = & 'kdir-cmdb.ps1' -被查找的库文件名 'invoke_系统库.ps1'
if ( ($private:temp999 -eq '输入的库路径有错误') -and ($private:temp999 -eq '无返回') )
{
	Write-Error "找不到 'invoke_系统库' "
	exit 1
}
else
{
	$invoke_系统库 = $private:temp999
}

$sqlite_sql =
@"
SELECT * FROM "k计划表" WHERE "启用状态"=1
;
"@


try
{
	$private:k库_所有_定时任务动作 = & $invoke_系统库  -sqlite_sql语句 $sqlite_sql
}
catch
{
	Write-Error '错误：查询【卡死你3000】定时任务失败'
	exit 2
}
finally
{

}
Write-Host '查询【卡死你3000】定时任务成功' -ForegroundColor Green

if ( ($private:k库_所有_定时任务动作 -eq $null) -or ($private:k库_所有_定时任务动作 -eq '') )
{
	Write-Verbose '信息：找不到k库，计划表，任何动作。'
	exit 3
}
else
{
	foreach ($private:每条定时任务 in $private:k库_所有_定时任务动作)
	{
		Write-Verbose "定时任务动作开始"
		${private:每条定时任务}
		$private:触发器1id = $private:每条定时任务['触发器1id']
		$sqlite_sql = @"
SELECT * FROM "k触发器表" WHERE "id"=${private:触发器1id}
;
"@

		try
		{
			$private:触发器1对象 = & $invoke_系统库  -sqlite_sql语句 $sqlite_sql
		}
		catch
		{
			Write-Error '错误：查询本条【定时任务动作】的，触发器1，执行失败'
			continue
		}
		finally
		{

		}
		Write-Host '查询本条【定时任务动作】的，触发器1，执行成功' -ForegroundColor Green

		if ( ($private:触发器1对象 -eq $null) -or ($private:触发器1对象 -eq '') )
		{
			Write-Verbose "错误：找不到k库，触发器表，中的这个触发器：。"
			${private:触发器1id}
			continue
		}
		else
		{
			if ($private:触发器1对象['启用状态'] -eq 0)
			{
				Write-Verbose "信息：触发器已经停用。"
				continue
			}
			else
			{
				Write-Verbose "开始解析触发器："
				${private:触发器1对象}
				if ($private:触发器1对象['月'] -eq 'n')
				{
					if ($private:触发器1对象['周'] -eq 'n')
					{
						Write-Error '错误：月和周都是n'
						continue
					}
					else
					{
						Write-Verbose "命中【周】触发器"
						$private:当前时间 = Get-Date
						if ([int]$($private:当前时间.DayOfWeek) -eq 0)
						{
							$private:周几 = 7
						}
						else
						{
							$private:周几 = [int]$($private:当前时间.DayOfWeek)
						}

						$private:触发器1_周 = $private:触发器1对象['周'].split(',')
						if ($private:周几 -in $private:触发器1_周)
						{
							Write-Verbose '命中【本周】触发器'
							$private:temp997 = '分钟_in_' + $private:当前时间.Hour + '小时'
							[string]$private:触发器1_分钟1 = $private:触发器1对象["$private:temp997"]
							$private:触发器1_分钟2 = $private:触发器1_分钟1.split(',')
							if ($private:当前时间.Minute -in $private:触发器1_分钟2)
							{
								Write-Verbose '命中【分钟】触发器'
								$private:触发器1秒_数组 = $private:触发器1对象['秒'].split(',')
								foreach ($private:触发器1单个秒 in $private:触发器1秒_数组)
								{
									[sbyte]$private:比触发器1小5秒 = 0 + $private:触发器1单个秒 - 5
									[sbyte]$private:比触发器1大5秒 = 0 + $private:触发器1单个秒 + 5
									Write-Verbose "u库，定时任务，起始时间，秒： $private:比触发器1小5秒"
									Write-Verbose "u库，定时任务，终止时间，秒： $private:比触发器1大5秒"
									Write-Verbose ("当前，秒：【{0}】" -f $private:当前时间.Second)
									if ( ($private:当前时间.Second -gt $private:比触发器1小5秒) -and ($private:当前时间.Second -lt $private:比触发器1大5秒) )
									{
										Write-Verbose "命中【秒】触发器"
										$private:temp998 = & 'kdir-cmdb.ps1' -被查找的库文件名 'k库_任务表_添加任务215.ps1'
										if ( ($private:temp998 -eq '输入的库路径有错误') -and ($private:temp998 -eq '无返回') )
										{
											Write-Error "找不到 'k库_任务表_添加任务215.ps1' "
											continue
										}
										else
										{
											$private:k库_任务表_添加任务215 = $private:temp998
										}

										if ( ($private:每条定时任务['被控机ip'] -eq '主控机') -or ($private:每条定时任务['被控机ip'] -eq 'master') )
										{
											if ($private:每条定时任务['powershell代码块'].trim() -eq '')
											{
												$private:命令行 = "& '" + $private:每条定时任务['脚本文件名'] + "' " + $private:每条定时任务['传入参数'].replace(',',' ')
											}
											else
											{
												$private:命令行 = '& {' + $private:每条定时任务['powershell代码块'] + '} ' + $private:每条定时任务['传入参数'].replace(',',' ')
											}
										}
										else
										{
											$win = 'win7','win8','win10','win2008r2','win2012r2','win2016','win2019'
											$linux = 'centos7','centos8','debian8','debian9','debian10','ubuntu1404','ubuntu1604','ubuntu1804','alpine'
											$all = $win + $linux

											if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
											{
												if ($private:每条定时任务['被控机os类型'] -in $win)
												{
													$private:命令行 = "& 'run_win2win5985_pwd.ps1' -目的ip地址 "
												}

												if ($private:每条定时任务['被控机os类型'] -in $linux)
												{
													$private:命令行 = "& 'run_win2linux_key_pwd.ps1' -目的ip地址 "
												}
											}

											if ($IsLinux -eq $True)
											{
												if ($private:每条定时任务['被控机os类型'] -in $win)
												{
													Write-Error "无法从【linux主控机】控制【win被控机】"
													continue
												}

												if ($private:每条定时任务['被控机os类型'] -in $linux)
												{
													$private:命令行 = "& 'run_linux2linux_key_pwd.ps1' -目的ip地址 "
												}
											}

											$private:命令行 = $private:命令行 + "'" + $private:每条定时任务['被控机ip'] + "' "

											if ($private:每条定时任务['powershell代码块'].trim() -eq '')
											{
												$private:命令行 = $private:命令行 + '-脚本文件名 ' + "'" + $private:每条定时任务['脚本文件名'] + "' "
											}
											else
											{
												$private:命令行 = $private:命令行 + '-powershell代码块 {' + $private:每条定时任务['powershell代码块'] + '} '
											}

											if ( ($private:每条定时任务['传入参数'].trim() -eq $null) -or ($private:每条定时任务['传入参数'].trim() -eq '') )
											{

											}
											else
											{
												$private:命令行 = $private:命令行 + "-传入参数 " + $private:每条定时任务['传入参数']
											}
										}

										& $private:k库_任务表_添加任务215 -被控机ip $private:每条定时任务['被控机ip'] -备注 $private:每条定时任务['备注'] -k库命令行 $private:命令行
									}
								}
							}
						}
					}
				}
				else
				{
					if ($private:触发器1对象['周'] -ne 'n')
					{
						Write-Error '错误：月和周都不是n！'
						continue
					}
					else
					{
						if ($private:触发器1对象['月'] -eq 'n')
						{
							Write-Error "错误：有月触发器，无日触发器。请检查id为【${private:触发器1id}】的触发器"
							continue
						}
						else
						{
							Write-Verbose "命中【月日】触发器"
							$private:当前时间 = Get-Date
							$private:触发器1_月 = $private:触发器1对象['月'].split(',')
							if ($private:当前时间.Month -in $private:触发器1_月)
							{
								Write-Verbose "命中【本月】触发器"
								$private:触发器1_日 = $private:触发器1对象['日'].split(',')
								if ((Get-Date).day -in $private:触发器1_日)
								{
									Write-Verbose "命中【本日】触发器"
									$private:temp996 = '分钟_in_' + $private:当前时间.Hour + '小时'
									[string]$private:触发器1_分钟1 = $private:触发器1对象["$private:temp996"]
									$private:触发器1_分钟2 = $private:触发器1_分钟1.split(',')
									if ($private:当前时间.Minute -in $private:触发器1_分钟2)
									{
										Write-Verbose '命中【分钟】触发器'
										$private:触发器1秒_数组 = $private:触发器1对象['秒'].split(',')
										foreach ($private:触发器1单个秒 in $private:触发器1秒_数组)
										{
											[sbyte]$private:比触发器1小5秒 = 0 + $private:触发器1单个秒 - 5
											[sbyte]$private:比触发器1大5秒 = 0 + $private:触发器1单个秒 + 5
											Write-Verbose "u库，定时任务，起始时间，秒： $private:比触发器1小5秒"
											Write-Verbose "u库，定时任务，终止时间，秒： $private:比触发器1大5秒"
											Write-Verbose ("当前，秒：【{0}】" -f $private:当前时间.Second)
											if ( ($private:当前时间.Second -gt $private:比触发器1小5秒) -and ($private:当前时间.Second -lt $private:比触发器1大5秒) )
											{
												Write-Verbose "命中【秒】触发器"
												$private:temp995 = & 'kdir-cmdb.ps1' -被查找的库文件名 'k库_任务表_添加任务215.ps1'
												if ( ($private:temp995 -eq '输入的库路径有错误') -and ($private:temp995 -eq '无返回') )
												{
													Write-Error "找不到 'k库_任务表_添加任务215.ps1' "
													continue
												}
												else
												{
													$private:k库_任务表_添加任务215 = $private:temp995
												}

												if ( ($private:每条定时任务['被控机ip'] -eq '主控机') -or ($private:每条定时任务['被控机ip'] -eq 'master') )
												{
													if ($private:每条定时任务['powershell代码块'].trim() -eq '')
													{
														$private:命令行 = "& '" + $private:每条定时任务['脚本文件名'] + "' " + $private:每条定时任务['传入参数'].replace(',',' ')
													}
													else
													{
														$private:命令行 = '& {' + $private:每条定时任务['powershell代码块'] + '} ' + $private:每条定时任务['传入参数'].replace(',',' ')
													}
												}
												else
												{
													$win = 'win7','win8','win10','win2008r2','win2012r2','win2016','win2019'
													$linux = 'centos7','centos8','debian8','debian9','debian10','ubuntu1404','ubuntu1604','ubuntu1804','alpine'
													$all = $win + $linux

													if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
													{
														if ($private:每条定时任务['被控机os类型'] -in $win)
														{
															$private:命令行 = "& 'run_win2win5985_pwd.ps1' -目的ip地址 "
														}

														if ($private:每条定时任务['被控机os类型'] -in $linux)
														{
															$private:命令行 = "& 'run_win2linux_key_pwd.ps1' -目的ip地址 "
														}
													}

													if ($IsLinux -eq $True)
													{
														if ($private:每条定时任务['被控机os类型'] -in $win)
														{
															Write-Error "无法从【linux主控机】控制【win被控机】"
															continue
														}

														if ($private:每条定时任务['被控机os类型'] -in $linux)
														{
															$private:命令行 = "& 'run_linux2linux_key_pwd.ps1' -目的ip地址 "
														}

														if ($private:每条定时任务['powershell代码块'].trim() -eq '')
														{
															$private:命令行 = $private:命令行 + '-脚本文件名 ' + "'" + $private:每条定时任务['脚本文件名'] + "' "
														}
														else
														{
															$private:命令行 = $private:命令行 + '-powershell代码块 {' + $private:每条定时任务['powershell代码块'] + '} '
														}
													}

													$private:命令行 = $private:命令行 + "'" + $private:每条定时任务['被控机ip'] + "' "

												}

												if ( ($private:每条定时任务['传入参数'].trim() -eq $null) -or ($private:每条定时任务['传入参数'].trim() -eq '') )
												{

												}
												else
												{
													$private:命令行 = $private:命令行 + "-传入参数 " + $private:每条定时任务['传入参数']
												}

												& $private:k库_任务表_添加任务215 -被控机ip $private:每条定时任务['被控机ip'] -备注 $private:每条定时任务['备注'] -k库命令行 $private:命令行
											}
										}
									}
								}
							}
						}
					}
				}

				if ($private:触发器1对象['启用状态'] -eq 2)
				{
					$private:temp994 = & 'kdir-cmdb.ps1' -被查找的库文件名 'k库_触发器表_禁用触发器244.ps1'
					if ( ($private:temp994 -eq '输入的库路径有错误') -and ($private:temp994 -eq '无返回') )
					{
						Write-Error "找不到 'k库_触发器表_禁用触发器244.ps1' "
						continue
					}
					else
					{
						$private:k库_触发器表_禁用触发器244 = $private:temp994
					}

					& $private:k库_触发器表_禁用触发器244 -id $private:触发器1对象['id']
				}
				Write-Verbose "完成解析触发器"
			}
		}
		Write-Verbose "定时任务动作完成"
	}
	exit 0
}
